//
//  TXJLViewController.m
//  FruitsWholesaler
//
//  Created by sgone-2 on 15/12/2.
//  Copyright © 2015年 sgone. All rights reserved.
//

#import "TXJLViewController.h"
#import "AFNetworking.h"
#import "MJRefresh.h"
#import "TXJLCell.h"
#import "MBProgressHUD+MJ.h"
#import "OMOrderEmptyTableViewCell.h"
@interface TXJLViewController ()<UITableViewDataSource,UITableViewDelegate>
@property (weak, nonatomic) IBOutlet UITableView *tabelView;

//加载数据btn
@property(nonatomic,strong) UIButton *btn;


@property(nonatomic,strong) NSMutableArray *list;
//网路请求
@property(nonatomic,strong)  AFHTTPSessionManager *manager;
@property (nonatomic,strong) OMOrderEmptyTableViewCell *emptyCell;
@property(nonatomic,assign)  CGFloat emptyCellHeight;


@end

@implementation TXJLViewController



static int cur_page = 1;


//afnetworking网络请求数据
- (AFHTTPSessionManager *)manager
{
    if (_manager == nil) {
        _manager  = [AFHTTPSessionManager manager];
    }
    return _manager;
    
    
}



-(void)viewWillAppear:(BOOL)animated
{

}




- (void)viewDidLoad {
    [super viewDidLoad];
    cur_page = 1;

    self.titleLabel.text = @"交易明细";
    self.backBtn.hidden = NO;
    self.list = [NSMutableArray arrayWithCapacity:0];
   
    
    [self   setupRefresh];
    
    [self sendAPI];

    
    
    
    
    

    

    
    
    
    
}



#pragma mark 进行刷新
-(void)setupRefresh
{


    // 2.上拉加载更多(进入刷新状态就会调用self的footerRereshing)
    [self.tabelView addFooterWithTarget:self action:@selector(footerRereshing1)];


    self.tabelView.footerPullToRefreshText = @"上拉可以加载更多数据";
    self.tabelView.footerReleaseToRefreshText = @"松开马上加载更多数据";
    self.tabelView.footerRefreshingText = @"正在帮你加载中";



}


- (void)footerRereshing1
{
    
    cur_page++;
   [self sendAPI2];
    // (最好在刷新表格后调用)调用endRefreshing可以结束刷新状态
    [self.tabelView footerEndRefreshing];
    
}



//加载更多的数据
-(void)sendAPI2

{

    NSString *urlString = [NSString stringWithFormat:@"%@get_wholesaler_io_record",SERVER_ADDRESS2];
    NSDictionary *parmas = @{@"wTel" : self.dataHandler.phone , @"uToken" : self.dataHandler.uToken , @"cur_page" : [NSString stringWithFormat:@"%@",[NSString stringWithFormat:@"%d",cur_page]]};
    
    [self.manager POST:urlString parameters:parmas success:^(NSURLSessionDataTask *task, id responseObject) {
       
        if ([responseObject[@"error_flag"] intValue] == 0) {
            
            NSArray *array = responseObject[@"data"][@"list"];
            
            
            if (array.count != 0)
            {
//                [self.list removeAllObjects];
//                
//                self.list = [NSMutableArray arrayWithArray:array];
                
                
                [self.list addObjectsFromArray:array];
                
                
            }
            else
            {
            
            
            
            
            }

            [self.tabelView footerEndRefreshing];
            [self.tabelView reloadData];
            

            
            
        }
        else
        {
        
            [self.tabelView footerEndRefreshing];
            [MBProgressHUD hideAllHUDsForView:self.tabelView animated:YES];
            NSString *str = [NSString stringWithFormat:@"%@",responseObject[@"result_msg"]];
            
            UIAlertView *alter = [[UIAlertView alloc]initWithTitle:@"提示" message:str delegate:nil cancelButtonTitle:nil otherButtonTitles:@"确定", nil];
            [alter show];
        
        }
        
        
        
    } failure:^(NSURLSessionDataTask *task, NSError *error) {
        
        [self.tabelView footerEndRefreshing];
        
        UIAlertView *alter = [[UIAlertView alloc]initWithTitle:@"提示" message:@"连接网络失败！" delegate:nil cancelButtonTitle:nil
                                             otherButtonTitles:@"确定", nil];
        [alter show];
        
    }];
    


}




#pragma mark 进行网络请求获取数据
-(void)sendAPI
{

    NSString *urlString = [NSString stringWithFormat:@"%@get_wholesaler_io_record",SERVER_ADDRESS2];
    NSDictionary *parmas = @{@"wTel" : self.dataHandler.phone , @"uToken" : self.dataHandler.uToken , @"cur_page" : [NSString stringWithFormat:@"%@",[NSString stringWithFormat:@"%d",cur_page]]};
    
    
    
    [MBProgressHUD showHUDAddedTo:self.tabelView animated:YES];
    [self.manager POST:urlString parameters:parmas success:^(NSURLSessionDataTask *task, id responseObject) {
        if ([responseObject[@"error_flag"] intValue] == 0) {

            
            
            NSArray *array = responseObject[@"data"][@"list"];
            
            
           if (array.count != 0)
            {
            
                self.list = [NSMutableArray arrayWithArray:array];
                
                
            }
            
//            NSLog(@"333333333333  %@",responseObject);
          

            [self.tabelView reloadData];
            [MBProgressHUD hideAllHUDsForView:self.tabelView animated:YES];
            
        }
        
        else
        {
            
           
            [MBProgressHUD hideAllHUDsForView:self.tabelView animated:YES];
            NSString *str = [NSString stringWithFormat:@"%@",responseObject[@"result_msg"]];
            
            UIAlertView *alter = [[UIAlertView alloc]initWithTitle:@"提示" message:str delegate:nil cancelButtonTitle:nil otherButtonTitles:@"确定", nil];
            [alter show];
            
            
            
        }

        
        
        
    } failure:^(NSURLSessionDataTask *task, NSError *error) {
        
        [MBProgressHUD hideAllHUDsForView:self.tabelView animated:YES];
        UIAlertView *alter = [[UIAlertView alloc]initWithTitle:@"提示" message:@"连接网络失败！" delegate:nil cancelButtonTitle:nil
                                             otherButtonTitles:@"确定", nil];
        [alter show];
        
        
    }];

}




-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{

    return 1;

}



-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{

    if (self.list.count == 0) {
        return 1;
    }
    
    return self.list.count;



}



-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{

    return 0.01;


}


/*

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{

    UIView *view2 = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 0, 0)];
    view2.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:view2];
    
    
    UIView *view3 = [[UIView alloc]initWithFrame:CGRectMake(0, 49, ViewWidth, 0.3)];
    view3.backgroundColor = [UIColor colorWithWhite:0.4 alpha:0.5];
    [view2 addSubview:view3];
    
    
    UILabel *lbl1 = [[UILabel alloc]initWithFrame:CGRectMake(15, 0, 100, 50)];
    lbl1.text = @"总共线上金额";
    lbl1.textColor = [UIColor grayColor];
    lbl1.font = [UIFont systemFontOfSize:16];
    [view2 addSubview:lbl1];
    
    
    
    UILabel *lbl12 = [[UILabel alloc]initWithFrame:CGRectMake(ViewWidth-120, 0, 110, 50)];
    lbl12.text = [NSString stringWithFormat:@"￥%@元",self.existing];
    lbl12.textAlignment = NSTextAlignmentRight;
    lbl12.textColor = [UIColor lightGrayColor];
    lbl12.font = [UIFont systemFontOfSize:16];
    [view2 addSubview:lbl12];
    
    
    
    
    UIView *view4 = [[UIView alloc]initWithFrame:CGRectMake(0, 99, ViewWidth, 0.3)];
    view4.backgroundColor = [UIColor colorWithWhite:0.4 alpha:0.5];
    [view2 addSubview:view4];
    
    
    UILabel *lbl2 = [[UILabel alloc]initWithFrame:CGRectMake(15, 50, 100, 50)];
    lbl2.text = @"总提现";
    lbl2.textColor = [UIColor grayColor];
    lbl2.font = [UIFont systemFontOfSize:16];
    [view2 addSubview:lbl2];
    
    UILabel *lbl21 = [[UILabel alloc]initWithFrame:CGRectMake(ViewWidth-120, 50, 110, 50)];
    lbl21.text = [NSString stringWithFormat:@"￥%@元",self.has_apply];
    lbl21.textAlignment = NSTextAlignmentRight;
    lbl21.textColor = [UIColor lightGrayColor];
    lbl21.font = [UIFont systemFontOfSize:16];
    [view2 addSubview:lbl21];
    
    
    
    
    UILabel *lbl3 = [[UILabel alloc]initWithFrame:CGRectMake(15, 100, 100, 50)];
    lbl3.text = @"当前余额";
    lbl3.textColor = [UIColor grayColor];
    lbl3.font = [UIFont systemFontOfSize:16];
    [view2 addSubview:lbl3];
    
    
    UILabel *lbl31 = [[UILabel alloc]initWithFrame:CGRectMake(ViewWidth-120, 100, 110, 50)];
    lbl31.text = [NSString stringWithFormat:@"￥%@元",self.could_apply];
    lbl31.textAlignment = NSTextAlignmentRight;
    lbl31.textColor = [UIColor lightGrayColor];
    lbl31.font = [UIFont systemFontOfSize:16];
    [view2 addSubview:lbl31];

    
    
    //添加空白
    UIView *view11 = [[UIView alloc]initWithFrame:CGRectMake(0, 150, ViewWidth, 30)];
    view11.backgroundColor = BACKGROUND1;
    [view2 addSubview:view11];

    //添加view的提示的提示
    
    UILabel *lbl41 = [[UILabel alloc]initWithFrame:CGRectMake(15, 0, 60, 30)];
    lbl41.text = @"事件";
    lbl41.font = [UIFont systemFontOfSize:15];
    lbl41.textColor = [UIColor grayColor];
    [view11 addSubview:lbl41];
    
    
    
    
    UILabel *lbl42 = [[UILabel alloc]initWithFrame:CGRectMake(ViewWidth/4 , 0, 80, 30)];
    lbl42.text = @"描述";
    lbl42.textAlignment = NSTextAlignmentCenter;
    lbl42.font = [UIFont systemFontOfSize:15];
    lbl42.textColor = [UIColor grayColor];
    [view11 addSubview:lbl42];
    
    
    
    
    
    UILabel *lbl43 = [[UILabel alloc]initWithFrame:CGRectMake(ViewWidth/4*2-10 , 0, 80, 30)];
    lbl43.text = @"时间";
    lbl43.textAlignment = NSTextAlignmentRight;
    lbl43.font = [UIFont systemFontOfSize:15];
    lbl43.textColor = [UIColor grayColor];
    [view11 addSubview:lbl43];

    
    
    
    
    UILabel *lbl44 = [[UILabel alloc]initWithFrame:CGRectMake(ViewWidth/4*3, 0, 70, 30)];
    lbl44.text = @"金额";
    lbl44.textAlignment = NSTextAlignmentRight;
    lbl44.font = [UIFont systemFontOfSize:15];
    lbl44.textColor = [UIColor grayColor];
    [view11 addSubview:lbl44];

    
    
    
    
    

    
    
    
    
    return view2;
    
    



}
*/




-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{


    return 25;

}

//
//-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
//{
//
//
//    UIView *view2 = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 10, 10)];
//    view2.backgroundColor = BACKGROUND2;
//    [self.view addSubview:view2];
//    
//    self.btn = [[UIButton alloc]initWithFrame:CGRectMake(ViewWidth/2-60, 0,120 , 25)];
//    [self.btn setTitle:@"点击加载更多数据" forState:UIControlStateNormal];
//    self.btn.titleLabel.font = [UIFont systemFontOfSize:15];
//    [self.btn setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
//    [self.btn addTarget:self action:@selector(setupRefresh) forControlEvents:UIControlEventTouchUpInside];
//    
//    [view2 addSubview:self.btn];
//    
//    
//    return view2;
//
//}



-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{

    if (self.list.count == 0) {
        
        return self.emptyCellHeight;
        
    }
    return 70;


}






-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{


    if (self.list.count == 0) {
        
        self.emptyCell = [OMOrderEmptyTableViewCell cellWithTableView:self.tabelView];
        self.emptyCell.selectionStyle = UITableViewCellSelectionStyleNone;
        self.emptyCellHeight = [self.emptyCell setEmptyOrderCell];
        self.emptyCell.backgroundColor = BACKGROUND1;
        return self.emptyCell;

    }
    
    
    static NSString *CellIdentifier = @"Cell";
    TXJLCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[NSBundle mainBundle] loadNibNamed:@"TXJLCell" owner:self options:nil][0];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        cell.backgroundColor = [UIColor whiteColor];

    }
    
    
    //数组传递到下一个页面
    NSDictionary *dict  = [self.list objectAtIndex:indexPath.row];

    [cell loadCell:dict];
    
    
    
    return cell;


}





@end
